package turtle.tools;

import generator.website.SimpleToHTML;

import java.awt.Desktop.Action;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

import turtle.ActionBase;
import turtle.ActionFoward;
import turtle.ActionRotate;
import turtle.ActionSetPen;
import turtle.Choreography;
import turtle.Direction;
import turtle.Farm;
import turtle.Stage;
import turtle.Turtle;
import turtle.TurtleFactory;
import turtle.TurtlePackage;
import turtle.impl.*;

public class TurtleSerialize {
	
	
	public static void generateHtmlDoc(Stage stage){
		//SimpleToHTML est le nom de la classe générée par JET à partir du template
		SimpleToHTML htmlpage = new SimpleToHTML();
		FileWriter output;
		BufferedWriter writer;
		System.out.println("Creating Stage HTML Documentation");
		try {
		output = new FileWriter("stageDoc.html");
		writer = new BufferedWriter(output);
		//Appel de la méthode generate de la classe générée par JET
		writer.write(htmlpage.generate(stage));
		writer.close();
		} catch (IOException e) {
		e.printStackTrace();
		}
		}

	public static Stage load(File f) {
		ResourceSet rs = new ResourceSetImpl();
		Resource.Factory.Registry registry = rs.getResourceFactoryRegistry();
		Map<String, Object> m = registry.getExtensionToFactoryMap();
		m.put("xmi", new XMIResourceFactoryImpl());
		rs.getPackageRegistry().put(TurtlePackage.eNS_URI,
				TurtlePackage.eINSTANCE);
		URI uri = URI.createFileURI(f.getAbsolutePath());
		Resource resource = rs.getResource(uri, true);
		if (resource.isLoaded() && resource.getContents().size() > 0) {
			return (Stage) resource.getContents().get(0);
		}
		return null;
	}

	public static void save(Stage stage, File f) {
		ResourceSet rs = new ResourceSetImpl();
		Resource.Factory.Registry registry = rs.getResourceFactoryRegistry();
		Map<String, Object> m = registry.getExtensionToFactoryMap();
		m.put("xmi", new XMIResourceFactoryImpl());
		m.put("ecore", new EcoreResourceFactoryImpl());
		rs.getPackageRegistry().put(TurtlePackage.eNS_URI,
				TurtlePackage.eINSTANCE);
		Resource packageResource = rs.createResource(URI
				.createFileURI("turtle.ecore"));
		packageResource.getContents().add(TurtlePackage.eINSTANCE);
		try {
			packageResource.load(null);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		URI uri = URI.createFileURI(f.getAbsolutePath());
		Resource resource = rs.createResource(uri);
		resource.getContents().add(stage);
		try {
			HashMap<String, Boolean> options = new HashMap<String, Boolean>();
			options.put(XMIResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
			resource.save(options);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String stagePath = "/home/nxhoaf/workspace/TurtleGraphics/model/Stage.xmi";
		File stageFile = new File(stagePath);
		Stage stage = load(stageFile);
		stage.display();
		
		// Question 2
		Farm farm = stage.getFarms().get(0);
		
		// Creer un nouvelle choreographie
		TurtleFactory turtleFactory = new TurtleFactoryImpl();
		Choreography choreography = turtleFactory.createChoreography();
		choreography.setName("Foward");
		
		// Creer des turtles
		Turtle turtle = turtleFactory.createTurtle();
		turtle.setName("Tata");
		farm.addTurtle(turtle);
		
		turtle = turtleFactory.createTurtle();
		turtle.setName("Toto");
		farm.addTurtle(turtle);
		
		// Creer de nouvelles actions		
		ActionBase action;	
		// La turtle qui va faire ça
		turtle = farm.getTurtles().get(1);
		
		// Rotate
		action = turtleFactory.createActionFoward();
		((ActionFoward)action).setLength(100);
		action.setTarget(turtle);
		choreography.addAction(action);
	
		// Set pen up
		action = turtleFactory.createActionSetPen();
		((ActionSetPen)action).setDirection(Direction.UP);
		action.setTarget(turtle);
		choreography.addAction(action);
		
		// Foward
		action = turtleFactory.createActionRotate();
		((ActionRotate)action).setAngle(45);
		action.setTarget(turtle);
		choreography.addAction(action);
		
		// Set pen down
		action = turtleFactory.createActionSetPen();
		((ActionSetPen)action).setDirection(Direction.DOWN);
		action.setTarget(turtle);
		choreography.addAction(action);
		
		
		// Relier toutes les choses au stage
		stage.addChoreography(choreography);
		String savedStagePath = "/home/nxhoaf/workspace/TurtleGraphics/model/savedModel.xmi";
		File savedStageFile = new File(savedStagePath);
		
//		String newFarmPath = "/home/nxhoaf/workspace/TurtleGraphics/model/Farm.xmi";
//		File newFarmFile = new File(newFarmPath);
		
		save(stage, savedStageFile);
		//save(farm, newFarmFile);
		
		System.out.println("Saved compete!");
		
		
		System.out.println("#########################################");
		generateHtmlDoc(stage);
	}
}
